

#ifndef PB_DEF_H__
#define PB_DEF_H__

#include <stdint.h>
#include <stdbool.h>

#define PB_MAGIC_CODE 			0xAB 
#define PB_HEADE_LENGTH			8 
#define PB_PAYLOAD_MAXLEN		(1024 + 20)

#define EPB_VER					0x00 

#define FIELD_COMMAND_INDEX     0
#define FIELD_LENGTH_INDEX      1 
#define FIELD_KEY_INDEX         2 
#define FIELD_DATA_INDEX        3 

#define FLAG_DIR_2HOST			0x08
#define FLAG_DIR_2DEV			0x00
#define FLAG_ACK				0x10 
#define FLAG_ERROR				0x20
#define FLAG_VERSION			EPB_VER
#define FLAG_CRYPTO_NONE		0x00 
#define FLAG_CRYPTO_MASK		0xC0 

#define FLAG_NORMAL_RESP		(0x00 | FLAG_VERSION ) 
#define FLAG_ERROR_RESP			(0x00 | FLAG_ERROR | FLAG_VERSION ) 
#define FLAG_ACK_RESP			(0x00 | FLAG_ACK | FLAG_VERSION )
#define FLAG_DEFAULT_RESP		(0x00 | FLAG_VERSION)

#define PB_FSM_IDLE     0
#define PB_FSM_PROGRESS     1
#define PB_FSM_PACKET_RX    2

typedef uint8_t pb_ret_code_t;


typedef enum{
	PB_RECV_PREAMBLE = 0, 
	PB_RECV_FLAG, 
	PB_RECV_LEN_LSB, 
	PB_RECV_LEN_MSB, 
	PB_RECV_CRC_LSB, 
	PB_RECV_CRC_MSB, 
	PB_RECV_TID_LSB, 
	PB_RECV_TID_MSB, 
	PB_RECV_COMMAND, 
	PB_RECV_PAYLOAD, 
	PB_RECV_XFER, 
	PB_RECV_XFER_PAYLOAD, 
	PB_RECV_XFER_WITHOUTFORMAT, 
}pb_recv_step_t; 

typedef enum{
    PBD_SUCCESS  = 0,   
    
    PBD_ERR_INVALID_LENGTH       ,      
    PBD_ERR_INVALID_FORMAT     ,        
    PBD_ERR_DATA_SIZE               ,   
    PBD_ERR_RESOURCE_INSUFFICIENT      ,
    PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED ,
    PBD_ERR_NO_MEM              ,       
    PBD_ERR_ADDR_RESP            ,      
    PBD_ERR_INVALID_KEY       ,         
    PBD_ERR_DELAY_NOT_EXPIRED_TIME     ,
    PBD_ERR_INVALID_STATE        ,      
    PBD_ERR_INVALID_PARAMS       ,      
    PBD_ERR_BUSY               ,        
	PBD_ERR_PERIPHERAL_FAIL_TO_SUPPORT, 
    PBD_ERR_PROG_ERROR, 
	PBD_ERR_SENSOR_IS_NOT_READY,

    PBD_ERR_UNKOWN             = 0xff      
}pb_err_code_t; 

typedef pb_err_code_t pbd_err_code_t ; 

typedef struct{
	uint8_t 	magic; 
	uint8_t 	flag; 
	uint16_t 	len; 
	uint16_t 	crc; 
	uint16_t 	id; 
	
	uint8_t 	state;
	uint16_t 	bytes_received; 
	uint8_t* const	p_data; // payload 
	
const	uint16_t 	max_len_support; 	
}pb_msg_t; 

typedef struct{
	uint8_t* 	p_data; 
	uint32_t 	size; 
}pb_stream_t; 

#define PB_MSG_RAW_VALUE(FLAG, LEN, DATA) \
	{\
		.flag = FLAG, \
		.len = LEN,\
		.p_data = DATA, \
	}
	
#define PB_MSG_INIT(max_len, data)	\
	{\
		.max_len_support = max_len, \
		.p_data = data,\
	}
		

#endif 

